﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="Web.WebForm4" %>

<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="Persits.Email" %>

<script runat="server" LANGUAGE="C#">

void Send(Object Source, EventArgs E)
{
	try
	{
		// MailSender object declaration
        MailSender objMail = new MailSender();

        // Set SMTP server address
        objMail.Host = txtHost.Text;
        
        // Set Username/Password if specified
        if (txtUsername.Text.Length > 0)
        {
            objMail.Username = txtUsername.Text;
            objMail.Password = txtPassword.Text;
        }

        // Enable TLS if so requested
        if (chkTls.Checked)
        {
            objMail.TLS = true;
        }
        
        // Sender's address
        objMail.From = txtFrom.Text;
        
        // Recipient's address
        objMail.AddAddress(txtTo.Text);
        
        // Unicode support
        objMail.ContentTransferEncoding = "Quoted-Printable";
        objMail.CharSet = "UTF-8";
        
        // Subject
        objMail.Subject = objMail.EncodeHeader(txtSubject.Text, "UTF-8");
        
        // Body
        objMail.Body = txtBody.Text;
        
        // HTML format?
        if (chkHTML.Checked)
        {
            objMail.IsHTML = true;
        }

        // Send message
        objMail.Send();

        txtResult.Text = "Success! Message has been sent to " + txtTo.Text;
	}
	catch(Exception ex)
	{
		txtResult.Text = "An error occurred: <font color=red>" + ex.Message + "</font>";
	}
}

</script>

<html>

<body>
    <h3>Unicode</h3>

    <form id="form1" runat="server">
    <table>
    <tr><td>Host:</td><td><asp:TextBox ID="txtHost" runat="server" Columns="50"/></td></tr>
    <tr><td>SMTP Username:</td><td><asp:TextBox ID="txtUsername" runat="server" Columns="50"/></td></tr>
    <tr><td>SMTP Password:</td><td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Columns="50"/></td></tr>
    <tr><td>Requires TLS?</td><td><asp:Checkbox ID="chkTls" runat="server"/></td></tr>
    <tr><td>From:</td><td><asp:TextBox ID="txtFrom" runat="server" Columns="50"/></td></tr>
    <tr><td>To:</td><td><asp:TextBox ID="txtTo" runat="server" Columns="50"/></td></tr>
    <tr><td>Subject:</td><td><asp:TextBox ID="txtSubject" runat="server" Columns="50"/></td></tr>
    <tr><td>Body:</td><td><asp:TextBox ID="txtBody" TextMode="MultiLine" Rows="5" runat="server" Columns="50"/></td></tr>
    <tr><td>HTML?</td><td><asp:Checkbox ID="chkHTML" runat="server"/></td></tr>
    <tr><td colspan="2"><asp:Button ID="Button1" runat="server" OnClick="Send" Text="Send Message"/></td></tr>
    </table>
    
    <p>
    <asp:Label ID="txtResult" runat="server"/>
    </p>
    
    </form>
</body>
</html>

